package com.xn.app.muying.adpter;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xn.app.muying.R;
import com.xn.app.muying.bean.DoctorListBean;
import com.xn.app.muying.view.RoundImageView;

import java.util.ArrayList;

/**
 * 医生列表
 * Created by Administrator on 2016/11/3.
 */
public class DoctorListAdapter extends BaseAdapter{
    private final LayoutInflater inflater;
    private Context context;
    private ArrayList<DoctorListBean> list = new ArrayList<>();
    private Handler handler;
    private ImageLoader imageLoader = null;
    private DisplayImageOptions options = null;

    public DoctorListAdapter(Context context, Handler handler, ImageLoader loader,
                             DisplayImageOptions options) {
        this.context = context;
        this.handler = handler;
        this.imageLoader = loader;
        this.options = options;
        inflater = LayoutInflater.from(context);
    }
    public void getData(ArrayList<DoctorListBean> data) {
        this.list=data;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if (view==null){
            view = inflater.inflate(R.layout.activity_doctorlist_item,null);
            holder = new ViewHolder();
            holder.tv_name = (TextView) view.findViewById(R.id.activity_doctorlist_name);
            holder.tv_title = (TextView) view.findViewById(R.id.activity_doctorlist_title);
            holder.tv_content = (TextView) view.findViewById(R.id.activity_doctorlist_content);
            holder.tv_hosptial_grade = (TextView) view.findViewById(R.id.activity_doctorlist_grade);
            holder.tv_hosptial_name = (TextView) view.findViewById(R.id.activity_doctorlist_doctorname);
            holder.iv = (RoundImageView) view.findViewById(R.id.activity_doctorlist_iv);
            view.setTag(holder);
        }else {
            holder = (ViewHolder) view.getTag();
        }
        holder.tv_name.setText(list.get(i).getName());
        holder.tv_title.setText(list.get(i).getProfessionalTitle());
        holder.tv_hosptial_grade.setText(list.get(i).getPrimaryHospitalLevel());
        holder.tv_hosptial_name.setText(list.get(i).getPrimaryHospitalName());
        holder.tv_content.setText("擅长:"+list.get(i).getSpecialty());
        holder.iv.setTag(list.get(i).getPhotoUrl());
        //设置图片，如果内存中有，优先从内存中读取
        if(list.get(i).getPhotoUrl()!=null&&!list.get(i).getPhotoUrl().equals("null")&&!list.get(i).getPhotoUrl().equals("")) {
            imageLoader.displayImage(list.get(i).getPhotoUrl(), holder.iv, options);
        }else {
            holder.iv.setImageResource(R.mipmap.doctor_no);
        }
        return view;
    }

    class ViewHolder{
        RoundImageView iv;
        TextView tv_name;
        TextView tv_title;
        TextView tv_hosptial_grade;
        TextView tv_hosptial_name;
        TextView tv_content;

    }
}
